package com.example.administrator.printoutindemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.adapter.OutStoreAdapter;
import com.example.administrator.printoutindemo.base.BaseFragment;
import com.example.administrator.printoutindemo.bean.OutKuListBean;
import com.example.administrator.printoutindemo.http.SPUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseConstants;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;

/**
 * Created by Administrator on 2018/9/10.
 */

public class OutFragment extends BaseFragment {

    private View view;
    private ListView lv_cklb;
    private ImageView tv_chaxunck;
    private EditText et_phone;
    private List<OutKuListBean> mlist = new ArrayList<>();
    private OutStoreAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_out, null);
        ToolbarUtils.initToolbarTitleNoBack(view, this, getString(R.string.chu_ku));

        lv_cklb = view.findViewById(R.id.lv_cklb);
        tv_chaxunck = view.findViewById(R.id.tv_chaxunck);
        et_phone = view.findViewById(R.id.et_phone);
        mAdapter = new OutStoreAdapter(getContext(), mlist);
        lv_cklb.setAdapter(mAdapter);
        tv_chaxunck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoadingDialogUtil.showGifdialog2(getFragmentManager(), R.drawable.public_loading);
                searchOutStorageList();
            }
        });
        return view;
    }

    private void searchOutStorageList() {


        String methodName = "SearchOutStorageList";

        SoapParams params = new SoapParams();
        params.put("userPhone", et_phone.getText().toString());
        params.put("uid", SPUtil.getString(getActivity(), "iduser", ""));

        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    mAdapter.shua();
                    Log.e("chuku=========", result);
                    List<OutKuListBean> outKuListBeen = new Gson().fromJson(result, new TypeToken<List<OutKuListBean>>() {
                    }.getType());
                    mAdapter.addAll(outKuListBeen);
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();
            }
        }, getAapiUserName(), getApiPwd());
    }
}
